package com.youhong.teethcare.plaque;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rmondjone.locktableview.DisplayUtil;
import com.youhong.teethcare.DatePickerActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.javabeans.PlaqueRecord;
import com.youhong.teethcare.plaque.dialogs.BleedingChooseDialog;
import com.youhong.teethcare.plaque.dialogs.BleedingDialog;
import com.youhong.teethcare.plaque.dialogs.PlaqueChooseDialog;
import com.youhong.teethcare.plaque.dialogs.PlaqueDialog;
import com.youhong.teethcare.plaque.dialogs.PocketChooseDialog;
import com.youhong.teethcare.plaque.dialogs.PocketDialog;
import com.youhong.teethcare.plaque.dialogs.ToothLocationDialog;
import com.youhong.teethcare.plaque.dialogs.ToothMobilityChooseDialog;
import com.youhong.teethcare.plaque.dialogs.ToothMobilityDialog;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.DBHelper;
import com.youhong.teethcare.utils.Util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class PlaqueChartActivity extends Activity implements View.OnClickListener {

    LinearLayout ll_container;
    ImageView iv_back;
    Calendar current_c;
    PlaqueTableView tableView;
    TextView tv_pcr, tv_bop;
    TextView tv_date;
    ImageView iv_dateLeft, iv_dateRight;

    List<PlaqueRecord> current_list;
    PlaqueRecord record1; //Plaque
    PlaqueRecord record2; //Mobility
    PlaqueRecord record3; //Bleeding
    PlaqueRecord record4; //Pocket

    private static final  int REQ_DATE_PICKER = 0x55AA;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plaque_chart);
        getViews();
        current_c = Calendar.getInstance();
        showData(current_c);

        //如果第一次进入该功能，显示功能提示。
        if (Common.userInfo.isFirstTimeDisplayPlaque() == true) {
            Common.userInfo.setFirstTimeDisplayPlaque(false);
            Common.userInfo.SyncToLocal(PlaqueChartActivity.this);
            
            PlaqueDialog dialog = new PlaqueDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
            dialog.setRunnable_understand(new Runnable() {
                @Override
                public void run() {

                    ToothMobilityDialog dialog = new ToothMobilityDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                    dialog.setRunnable_understand(new Runnable() {
                        @Override
                        public void run() {
                            BleedingDialog dialog = new BleedingDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                            dialog.setRunnable_understand(new Runnable() {
                                @Override
                                public void run() {

                                    ToothLocationDialog dialog = new ToothLocationDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                                    dialog.show();
                                }
                            });
                            dialog.show();
                        }
                    });
                    dialog.show();
                }
            });
            dialog.show();
        }
    }


    private void showData(Calendar calendar) {

        //显示当前日期
        tv_date.setText(Util.FormDateTimeString2(calendar));
        List<PlaqueRecord> list = DBHelper.getDbHelper(this).queryPlaqueRecordByDay(calendar);

        if (list.size() != 0) {
            current_list = list;
            for (PlaqueRecord record : list) {
                if (record.getType() == 0) {
                    record1 = record;

                } else if (record.getType() == 1) {
                    record2 = record;

                } else if (record.getType() == 2) {

                    record3 = record;
                } else {

                    record4 = record;
                }
            }

        } else {

            current_list = new ArrayList<>();
            record1 = new PlaqueRecord();
            record1.setDate((Calendar) current_c.clone());
            record1.setType(0);

            record2 = new PlaqueRecord();
            record2.setDate((Calendar) current_c.clone());
            record2.setType(1);

            record3 = new PlaqueRecord();
            record3.setDate((Calendar) current_c.clone());
            record3.setType(2);

            record4 = new PlaqueRecord();
            record4.setDate((Calendar) current_c.clone());
            record4.setType(3);

            current_list.add(record1);
            current_list.add(record2);
            current_list.add(record3);
            current_list.add(record4);
        }
        tableView.setValue(current_list);
        calculatePCRAndBOP();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DBHelper.getDbHelper(this).deletePlaqueRecordByDay(current_c);
        DBHelper.getDbHelper(this).insertPlaqueRecord(current_list);
    }

    //最后一次点击的cellView 的row 和 column
    int column = 0;
    int row = 0;

    View.OnClickListener cell_onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String[] index = v.getTag().toString().split(",");
            row = Integer.parseInt(index[0]);
            column = Integer.parseInt(index[1]);

            if (row == 0 || row == 7) {

                PlaqueChooseDialog dialog = new PlaqueChooseDialog(PlaqueChartActivity.this, R.style.dialog_choose,
                        PlaqueChooseDialog.TYPE_NORMAL
                );
                dialog.setCallback(new PlaqueChooseDialog.PlaqueOnClickCallback() {
                    @Override
                    public void onCallback(int type) {

                        if (type == PlaqueChooseDialog.TYPE_NORMAL) {

                            tableView.setCellViewBackground(row, column, PlaqueTableView.TYPE_NORMAL);
                            setValue(row, column, "0");

                        } else {

                            tableView.setCellViewBackground(row, column, PlaqueTableView.TYPE_PLAQUE);
                            setValue(row, column, "1");
                        }
                        calculatePCRAndBOP();
                        DBHelper.getDbHelper(PlaqueChartActivity.this).deletePlaqueRecordByDay(current_c);
                        DBHelper.getDbHelper(PlaqueChartActivity.this).insertPlaqueRecord(current_list);
                    }
                });
                dialog.show();

            } else if (row == 1 || row == 6) {

                ToothMobilityChooseDialog dialog = new ToothMobilityChooseDialog(PlaqueChartActivity.this, R.style.dialog_choose);
                dialog.setCallback(new ToothMobilityChooseDialog.MobilityOnClickCallback() {
                    @Override
                    public void onCallback(int value) {
                        tableView.getCellView(row, column).setText("" + value);
                        setValue(row, column, "" + value);
                        DBHelper.getDbHelper(PlaqueChartActivity.this).deletePlaqueRecordByDay(current_c);
                        DBHelper.getDbHelper(PlaqueChartActivity.this).insertPlaqueRecord(current_list);

                    }
                });
                dialog.show();
            } else if (row == 2 || row == 5) {

                BleedingChooseDialog dialog = new BleedingChooseDialog(PlaqueChartActivity.this, R.style.dialog_choose);
                dialog.setCallback(new BleedingChooseDialog.BleedingOnClickCallback() {
                    @Override
                    public void onCallback(int type) {

                        tableView.setCellViewBackground(row, column, type);
                        setValue(row, column, "" + type);
                        calculatePCRAndBOP();
                        DBHelper.getDbHelper(PlaqueChartActivity.this).deletePlaqueRecordByDay(current_c);
                        DBHelper.getDbHelper(PlaqueChartActivity.this).insertPlaqueRecord(current_list);
                    }
                });
                dialog.show();

            } else if (row == 3 || row == 4) {
                PocketChooseDialog dialog = new PocketChooseDialog(PlaqueChartActivity.this, R.style.dialog_choose);
                dialog.setCallback(new PocketChooseDialog.PlaqueOnClickCallback() {
                    @Override
                    public void onCallback(String text) {
                        tableView.getCellView(row, column).setText(text + "");
                        if (text.equals("n/a")) {
                            tableView.getCellView(row, column).setTextColor(getResources().getColor(R.color.seperateLineColor));
                            setValue(row, column, "n/a");

                        } else {
                            tableView.getCellView(row, column).setTextColor(getResources().getColor(R.color.black));
                            setValue(row, column, text);
                        }
                        DBHelper.getDbHelper(PlaqueChartActivity.this).deletePlaqueRecordByDay(current_c);
                        DBHelper.getDbHelper(PlaqueChartActivity.this).insertPlaqueRecord(current_list);

                    }
                });
                dialog.show();
            }

            /*if (v.getTag().toString().equals("0,0")) {

                BleedingDialog dialog = new BleedingDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                dialog.show();

            } else if (v.getTag().toString().equals("0,1")) {

                PlaqueChooseDialog dialog = new PlaqueChooseDialog(PlaqueChartActivity.this, R.style.dialog_choose);
                dialog.show();
            } else if (v.getTag().toString().equals("1,0")) {

                PlaqueDialog dialog = new PlaqueDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                dialog.show();
            } else if (v.getTag().toString().equals("1,1")) {

                BleedingChooseDialog dialog = new BleedingChooseDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                dialog.show();

            } else if (v.getTag().toString().equals("2,0")) {

                ToothLocationDialog dialog = new ToothLocationDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                dialog.show();

            } else if (v.getTag().toString().equals("2,1")) {
                ToothMobilityChooseDialog dialog = new ToothMobilityChooseDialog(PlaqueChartActivity.this, R.style.dialog_choose);
                dialog.show();


            } else if (v.getTag().toString().equals("3,0")) {

                PocketDialog dialog = new PocketDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                dialog.show();

            } else if (v.getTag().toString().equals("3,1")) {

                PocketChooseDialog dialog = new PocketChooseDialog(PlaqueChartActivity.this, R.style.dialog_choose);
                dialog.show();

            } else if (v.getTag().toString().equals("4,0")) {

                ToothMobilityDialog dialog = new ToothMobilityDialog(PlaqueChartActivity.this, R.style.dialog_bleeding);
                dialog.show();
            }*/

        }
    };

    private void setValue(int row, int column, String value) {

        if (row == 0) {
            switch (column) {
                case 0:
                    record1.setValue1(value);
                    break;
                case 1:
                    record1.setValue2(value);
                    break;
                case 2:
                    record1.setValue3(value);
                    break;
                case 3:
                    record1.setValue4(value);
                    break;
                case 4:
                    record1.setValue5(value);
                    break;
                case 5:
                    record1.setValue6(value);
                    break;
                case 6:
                    record1.setValue7(value);
                    break;
                case 7:
                    record1.setValue8(value);
                    break;
                case 8:
                    record1.setValue9(value);
                    break;
                case 9:
                    record1.setValue10(value);
                    break;
                case 10:
                    record1.setValue11(value);
                    break;
                case 11:
                    record1.setValue12(value);
                    break;
                case 12:
                    record1.setValue13(value);
                    break;
                case 13:
                    record1.setValue14(value);
                    break;
                case 14:
                    record1.setValue15(value);
                    break;
                case 15:
                    record1.setValue16(value);
                    break;
            }

        } else if (row == 7) {

            switch (column) {
                case 0:
                    record1.setValue17(value);
                    break;

                case 1:
                    record1.setValue18(value);
                    break;
                case 2:
                    record1.setValue19(value);
                    break;
                case 3:
                    record1.setValue20(value);
                    break;
                case 4:
                    record1.setValue21(value);
                    break;
                case 5:
                    record1.setValue22(value);
                    break;
                case 6:
                    record1.setValue23(value);
                    break;
                case 7:
                    record1.setValue24(value);
                    break;
                case 8:
                    record1.setValue25(value);
                    break;
                case 9:
                    record1.setValue26(value);
                    break;
                case 10:
                    record1.setValue27(value);
                    break;
                case 11:
                    record1.setValue28(value);
                    break;
                case 12:
                    record1.setValue29(value);
                    break;
                case 13:
                    record1.setValue30(value);
                    break;
                case 14:
                    record1.setValue31(value);
                    break;
                case 15:
                    record1.setValue32(value);
                    break;
            }

        } else if (row == 1) {

            switch (column) {
                case 0:
                    record2.setValue1(value);
                    break;

                case 1:
                    record2.setValue2(value);
                    break;
                case 2:
                    record2.setValue3(value);
                    break;
                case 3:
                    record2.setValue4(value);
                    break;
                case 4:
                    record2.setValue5(value);
                    break;
                case 5:
                    record2.setValue6(value);
                    break;
                case 6:
                    record2.setValue7(value);
                    break;
                case 7:
                    record2.setValue8(value);
                    break;
                case 8:
                    record2.setValue9(value);
                    break;
                case 9:
                    record2.setValue10(value);
                    break;
                case 10:
                    record2.setValue11(value);
                    break;
                case 11:
                    record2.setValue12(value);
                    break;
                case 12:
                    record2.setValue13(value);
                    break;
                case 13:
                    record2.setValue14(value);
                    break;
                case 14:
                    record2.setValue15(value);
                    break;
                case 15:
                    record2.setValue16(value);
                    break;
            }
        } else if (row == 6) {
            switch (column) {
                case 0:
                    record2.setValue17(value);
                    break;

                case 1:
                    record2.setValue18(value);
                    break;
                case 2:
                    record2.setValue19(value);
                    break;
                case 3:
                    record2.setValue20(value);
                    break;
                case 4:
                    record2.setValue21(value);
                    break;
                case 5:
                    record2.setValue22(value);
                    break;
                case 6:
                    record2.setValue23(value);
                    break;
                case 7:
                    record2.setValue24(value);
                    break;
                case 8:
                    record2.setValue25(value);
                    break;
                case 9:
                    record2.setValue26(value);
                    break;
                case 10:
                    record2.setValue27(value);
                    break;
                case 11:
                    record2.setValue28(value);
                    break;
                case 12:
                    record2.setValue29(value);
                    break;
                case 13:
                    record2.setValue30(value);
                    break;
                case 14:
                    record2.setValue31(value);
                    break;
                case 15:
                    record2.setValue32(value);
                    break;
            }
        } else if (row == 2) {

            switch (column) {
                case 0:
                    record3.setValue1(value);
                    break;

                case 1:
                    record3.setValue2(value);
                    break;
                case 2:
                    record3.setValue3(value);
                    break;
                case 3:
                    record3.setValue4(value);
                    break;
                case 4:
                    record3.setValue5(value);
                    break;
                case 5:
                    record3.setValue6(value);
                    break;
                case 6:
                    record3.setValue7(value);
                    break;
                case 7:
                    record3.setValue8(value);
                    break;
                case 8:
                    record3.setValue9(value);
                    break;
                case 9:
                    record3.setValue10(value);
                    break;
                case 10:
                    record3.setValue11(value);
                    break;
                case 11:
                    record3.setValue12(value);
                    break;
                case 12:
                    record3.setValue13(value);
                    break;
                case 13:
                    record3.setValue14(value);
                    break;
                case 14:
                    record3.setValue15(value);
                    break;
                case 15:
                    record3.setValue16(value);
                    break;
            }

        } else if (row == 5) {

            switch (column) {
                case 0:
                    record3.setValue17(value);
                    break;

                case 1:
                    record3.setValue18(value);
                    break;
                case 2:
                    record3.setValue19(value);
                    break;
                case 3:
                    record3.setValue20(value);
                    break;
                case 4:
                    record3.setValue21(value);
                    break;
                case 5:
                    record3.setValue22(value);
                    break;
                case 6:
                    record3.setValue23(value);
                    break;
                case 7:
                    record3.setValue24(value);
                    break;
                case 8:
                    record3.setValue25(value);
                    break;
                case 9:
                    record3.setValue26(value);
                    break;
                case 10:
                    record3.setValue27(value);
                    break;
                case 11:
                    record3.setValue28(value);
                    break;
                case 12:
                    record3.setValue29(value);
                    break;
                case 13:
                    record3.setValue30(value);
                    break;
                case 14:
                    record3.setValue31(value);
                    break;
                case 15:
                    record3.setValue32(value);
                    break;
            }
        } else if (row == 3) {

            switch (column) {
                case 0:
                    record4.setValue1(value);
                    break;

                case 1:
                    record4.setValue2(value);
                    break;
                case 2:
                    record4.setValue3(value);
                    break;
                case 3:
                    record4.setValue4(value);
                    break;
                case 4:
                    record4.setValue5(value);
                    break;
                case 5:
                    record4.setValue6(value);
                    break;
                case 6:
                    record4.setValue7(value);
                    break;
                case 7:
                    record4.setValue8(value);
                    break;
                case 8:
                    record4.setValue9(value);
                    break;
                case 9:
                    record4.setValue10(value);
                    break;
                case 10:
                    record4.setValue11(value);
                    break;
                case 11:
                    record4.setValue12(value);
                    break;
                case 12:
                    record4.setValue13(value);
                    break;
                case 13:
                    record4.setValue14(value);
                    break;
                case 14:
                    record4.setValue15(value);
                    break;
                case 15:
                    record4.setValue16(value);
                    break;
            }

        } else if (row == 4) {

            switch (column) {
                case 0:
                    record4.setValue17(value);
                    break;

                case 1:
                    record4.setValue18(value);
                    break;
                case 2:
                    record4.setValue19(value);
                    break;
                case 3:
                    record4.setValue20(value);
                    break;
                case 4:
                    record4.setValue21(value);
                    break;
                case 5:
                    record4.setValue22(value);
                    break;
                case 6:
                    record4.setValue23(value);
                    break;
                case 7:
                    record4.setValue24(value);
                    break;
                case 8:
                    record4.setValue25(value);
                    break;
                case 9:
                    record4.setValue26(value);
                    break;
                case 10:
                    record4.setValue27(value);
                    break;
                case 11:
                    record4.setValue28(value);
                    break;
                case 12:
                    record4.setValue29(value);
                    break;
                case 13:
                    record4.setValue30(value);
                    break;
                case 14:
                    record4.setValue31(value);
                    break;
                case 15:
                    record4.setValue32(value);
                    break;
            }
        }
    }

    private void getViews() {
        initDisplayOpinion();

        ll_container = findViewById(R.id.plaqueChart_container);
        tableView = new PlaqueTableView(PlaqueChartActivity.this, ll_container);
        tableView.setmOnclickLiastener(cell_onclick);
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlaqueChartActivity.this.finish();
            }
        });
        tableView.show();
        tv_bop = findViewById(R.id.plaqueChart_tv_bop);
        tv_pcr = findViewById(R.id.plaqueChart_tv_pcr);

        tv_date = findViewById(R.id.tableView_tv_date);
        tv_date.setOnClickListener(this);

        iv_dateLeft = findViewById(R.id.tableView_iv_left);
        iv_dateLeft.setOnClickListener(this);

        iv_dateRight = findViewById(R.id.tableView_iv_right);
        iv_dateRight.setOnClickListener(this);

    }

    private void calculatePCRAndBOP() {

        float plaque_count = 0, bleeding_count = 0;

        plaque_count += (record1.getValue1() == null || record1.getValue1().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue2() == null || record1.getValue2().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue3() == null || record1.getValue3().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue4() == null || record1.getValue4().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue5() == null || record1.getValue5().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue6() == null || record1.getValue6().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue7() == null || record1.getValue7().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue8() == null || record1.getValue8().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue9() == null || record1.getValue9().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue10() == null || record1.getValue10().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue11() == null || record1.getValue11().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue12() == null || record1.getValue12().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue13() == null || record1.getValue13().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue14() == null || record1.getValue14().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue15() == null || record1.getValue15().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue16() == null || record1.getValue16().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue17() == null || record1.getValue17().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue18() == null || record1.getValue18().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue19() == null || record1.getValue19().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue20() == null || record1.getValue20().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue21() == null || record1.getValue21().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue22() == null || record1.getValue22().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue23() == null || record1.getValue23().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue24() == null || record1.getValue24().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue25() == null || record1.getValue25().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue26() == null || record1.getValue26().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue27() == null || record1.getValue27().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue28() == null || record1.getValue28().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue29() == null || record1.getValue29().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue30() == null || record1.getValue30().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue31() == null || record1.getValue31().equals("0")) ? 0 : 1;
        plaque_count += (record1.getValue32() == null || record1.getValue32().equals("0")) ? 0 : 1;


        bleeding_count += (record3.getValue1() == null || record3.getValue1().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue2() == null || record3.getValue2().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue3() == null || record3.getValue3().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue4() == null || record3.getValue4().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue5() == null || record3.getValue5().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue6() == null || record3.getValue6().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue7() == null || record3.getValue7().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue8() == null || record3.getValue8().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue9() == null || record3.getValue9().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue10() == null || record3.getValue10().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue11() == null || record3.getValue11().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue12() == null || record3.getValue12().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue13() == null || record3.getValue13().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue14() == null || record3.getValue14().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue15() == null || record3.getValue15().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue16() == null || record3.getValue16().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue17() == null || record3.getValue17().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue18() == null || record3.getValue18().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue19() == null || record3.getValue19().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue20() == null || record3.getValue20().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue21() == null || record3.getValue21().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue22() == null || record3.getValue22().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue23() == null || record3.getValue23().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue24() == null || record3.getValue24().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue25() == null || record3.getValue25().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue26() == null || record3.getValue26().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue27() == null || record3.getValue27().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue28() == null || record3.getValue28().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue29() == null || record3.getValue29().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue30() == null || record3.getValue30().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue31() == null || record3.getValue31().equals("0")) ? 0 : 1;
        bleeding_count += (record3.getValue32() == null || record3.getValue32().equals("0")) ? 0 : 1;

        tv_bop.setText((int) ((bleeding_count / 32f) * 100) + " %");
        tv_pcr.setText((int) ((plaque_count / 32f) * 100) + " %");
    }

    private void initDisplayOpinion() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        DisplayUtil.density = dm.density;
        DisplayUtil.densityDPI = dm.densityDpi;
        DisplayUtil.screenWidthPx = dm.widthPixels;
        DisplayUtil.screenhightPx = dm.heightPixels;
        DisplayUtil.screenWidthDip = DisplayUtil.px2dip(getApplicationContext(), dm.widthPixels);
        DisplayUtil.screenHightDip = DisplayUtil.px2dip(getApplicationContext(), dm.heightPixels);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode == Activity.RESULT_OK  && requestCode == REQ_DATE_PICKER){

            Calendar c = (Calendar) data.getSerializableExtra("date");
            current_c = (Calendar) c.clone();
            showData(current_c);

        }
    }

    @Override
    public void onClick(View v) {

        if (v == iv_dateLeft) {

            current_c.add(Calendar.DAY_OF_YEAR, -1);
            showData(current_c);

        } else if (v == iv_dateRight) {

            //不能超过今天
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, 1);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);

            if (current_c.compareTo(c) > 0) {
                return;
            }
            current_c.add(Calendar.DAY_OF_YEAR, 1);
            showData(current_c);
        }else if(v == tv_date){


                Intent intent = new Intent(PlaqueChartActivity.this, DatePickerActivity.class);
                intent.putExtra("date", current_c);
                startActivityForResult(intent, REQ_DATE_PICKER);

        }
    }
}
